﻿import mx.utils.Delegate;
import arkadium.EventManager;
import arkadium.SoundManager;

class arkadium.MusicBtn extends MovieClip
{
	private var _sbtMusic:Button;
	private var _fCurrentWaitingEvent:Function;
	private var _cSoundManager:SoundManager;
	
	public function MusicBtn()
	{

	}
	
	public function onInitialize(cSoundManager){
		_cSoundManager = cSoundManager
		//trace("INNITED MUSIC BTN");
		if(_cSoundManager.getMusicState() == false) setBtnStateOff();
		else setBtnStateOn();

	}
	
	private function pressSoundBtn(){
		_cSoundManager.toggleMusicOn();
		_cSoundManager.playSoundName("clickSound");
			
	}
	
	private function setBtnStateOff(){
		this.gotoAndStop("off");
		//To keep from having creating memory leaks it's important to clear these listeners before adding new ones
		if(_fCurrentWaitingEvent != null)_cSoundManager._cEventManager.removeEventListener( "TURN_MUSIC_OFF", _fCurrentWaitingEvent ); 
		_cSoundManager._cEventManager.addEventListener( "TURN_MUSIC_ON", _fCurrentWaitingEvent = Delegate.create( this, setBtnStateOn ) ); 
		
		_sbtMusic.onRelease = Delegate.create( this, pressSoundBtn );
		_sbtMusic.onRollOver = Delegate.create( _cSoundManager, _cSoundManager.rolloverSoundStart ); // Damian: delegate
	}
	
	private function setBtnStateOn(){
		this.gotoAndStop("on");
		//To keep from having creating memory leaks it's important to clear these listeners before adding new ones
		if(_fCurrentWaitingEvent != null)_cSoundManager._cEventManager.removeEventListener( "TURN_MUSIC_ON", _fCurrentWaitingEvent ); 
		_cSoundManager._cEventManager.addEventListener( "TURN_MUSIC_OFF", _fCurrentWaitingEvent = Delegate.create( this, setBtnStateOff ) );
		
		_sbtMusic.onRelease = Delegate.create( this, pressSoundBtn );
		_sbtMusic.onRollOver = Delegate.create( _cSoundManager, _cSoundManager.rolloverSoundStart ); // Damian: delegate
		
	}

	
}